package com.hzlj.position.locate.controller.notify;


import cn.hutool.core.bean.BeanUtil;
import com.fz.common.base.core.JsonUtils;
import com.fz.common.base.dto.ResultDTO;
import com.hzlj.common.message.common.dto.common.AudioMailDTO;
import com.hzlj.position.locate.common.dto.reportAudio.ReportAudioPhoneAddDTO;
import com.hzlj.position.locate.service.ReportAudioService;
import com.hzlj.sqjz.config.common.dto.file.FileAddDTO;
import com.hzlj.sqjz.config.common.dto.file.FileQueryResultDTO;
import com.hzlj.sqjz.config.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 【通信联络核查-电话核查】接口
 *
 * @author makejava
 * @date 2024-04-22 11:16:28
 */
@Slf4j
@RestController
@RequestMapping("/position/report/audio/notify")
public class ReportAudioNotifyController {
    @Resource
    private ReportAudioService reportAudioService;
    @Resource
    private FileService fileService;

    /**
     * 语音电话汇报
     */
    @PutMapping("/phone")
    public ResultDTO<Boolean> reportPhone(@RequestBody List<AudioMailDTO> dtos) {
        log.info("【电话留言上报】语音电话汇报：dtos={}", JsonUtils.obj2json(dtos));
        List<ReportAudioPhoneAddDTO> adds = dtos.stream().map(e -> {
            try {
                //先保存文件
                FileAddDTO fileAdd = BeanUtil.copyProperties(e.getFile(), FileAddDTO.class);
                fileAdd.setFileName(e.getFile().getFullFileName());
                FileQueryResultDTO file = fileService.saveFile(fileAdd);
                //设置上报信息
                ReportAudioPhoneAddDTO add = new ReportAudioPhoneAddDTO();
                add.setJzId(e.getUser().getUserId());
                add.setSjh(e.getSjh());
                add.setFileIds(file.getId());
                add.setAudioDuration(e.getDuration());
                return add;
            } catch (Exception ee) {
                log.warn("【电话留言上报】电话核查，保存数据失败：dto={}", JsonUtils.obj2json(e), ee);
            }
            return null;
        }).filter(Objects::nonNull).collect(Collectors.toList());
        this.reportAudioService.report(adds);
        return new ResultDTO<>();
    }

}

